//
//  QXAdvertManager.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/5.
//

#import "QXAdvertManager.h"
#import <AdSupport/AdSupport.h>
#import <YYModel/YYModel.h>

#import <GDTMobSDK/GDTSDKConfig.h>
#import <GDTMobSDK/GDTUnifiedBannerView.h>
#import <GDTMobSDK/GDTSplashAd.h>
#import <GDTMobSDK/GDTUnifiedInterstitialAd.h>
#import <BUAdSDK/BUAdSDK.h>

#import "QXConfig.h"
#import "QXAdvert.h"
#import "QXAdHandler.h"

#import "QXSplashAdView.h"
#import "QXBannerAdView.h"
#import "QXFullScreenAdView.h"


#define MainBlock(block)  dispatch_async(dispatch_get_main_queue(),block)


@class BUNativeExpressSplashView;
@interface QXAdvertManager ()<BUNativeExpressFullscreenVideoAdDelegate,BUNativeExpressSplashViewDelegate,BUNativeExpressBannerViewDelegate,GDTSplashAdDelegate,GDTUnifiedBannerViewDelegate,GDTUnifiedInterstitialAdDelegate,QXBannerViewDelegate,QXFullScreenAdViewDelegate,QXSplashAdDelegate>{
    
}

@property(nonatomic,strong)NSMutableArray<QXConfig*>* configs;

//启动广告
@property (nonatomic, strong,nullable) id splashAd;

//视频广告
@property (nonatomic, strong,nullable) id videoAd;

//banner广告
@property (nonatomic, strong,nullable) id bannerAd;


//启动广告
@property (nonatomic, weak,nullable) UIViewController *splashController;

//视频广告
@property (nonatomic, weak,nullable) UIViewController *videoController;

//banner广告
@property (nonatomic, weak,nullable) UIViewController *bannerController;

//banner父视图
@property (nonatomic, weak,nullable) UIView *bannerSuper;


@end

@implementation QXAdvertManager

+ (QXAdvertManager *)manager{
    static dispatch_once_t once;
    static QXAdvertManager *manager = nil;
    dispatch_once(&once, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

- (instancetype)init{
    if(self = [super init]){
        _configs = [NSMutableArray array];
    }
    return self;
}

/// 初始化广告
/// @param channel 渠道
/// @param appId appid
/// @param sAdId 闪屏广告id
/// @param vAdId 全屏视频广告id
/// @param bAdid banner广告id
- (void)initSDKWithChannel:(ADChannel)channel appId:(NSString*)appId sAdId:(NSString*)sAdId vAdId:(NSString*)vAdId bAdid:(NSString*)bAdid{
    
    //避免重复初始化
    [self.configs enumerateObjectsUsingBlock:^(QXConfig*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(obj.channel == channel){
            [self.configs removeObjectAtIndex:idx];
            *stop = YES;
        }
    }];

    switch (channel) {
        case ADChannelSElF:{
            [QXConfig defaultConfig].channel = channel;
            [QXConfig defaultConfig].appid = appId;
            [QXConfig defaultConfig].sAdId = sAdId;
            [QXConfig defaultConfig].vAdId = vAdId;
            [QXConfig defaultConfig].bAdId = bAdid;
        }
        case ADChannelCSJ:{
            //穿山甲
            BOOL isIpad = [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad;
            [BUAdSDKManager setIsPaidApp:isIpad];
            [BUAdSDKManager setAppID:appId];
            [BUAdSDKManager startWithSyncCompletionHandler:^(BOOL success, NSError *error) {}];
            
            QXConfig* config = [QXConfig new];
            config.channel = channel;
            config.appid = appId;
            config.sAdId = sAdId;
            config.vAdId = vAdId;
            config.bAdId = bAdid;
            [self.configs addObject:config];
        }
            break;
        case ADChannelYLH:{
            //优量汇
            [GDTSDKConfig registerAppId:appId];
            [GDTSDKConfig setChannel:14];
            [GDTSDKConfig enableDefaultAudioSessionSetting:YES];
            
            QXConfig* config = [QXConfig new];
            config.channel = channel;
            config.appid = appId;
            config.sAdId = sAdId;
            config.vAdId = vAdId;
            config.bAdId = bAdid;
            [self.configs addObject:config];
        }
            break;
        default:
            break;
    }
}

//获取配置信息
- (QXConfig*)configWithChannel:(ADChannel)channel{
    __block QXConfig* config;
    [self.configs enumerateObjectsUsingBlock:^(QXConfig * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(obj.channel == channel){
            config = obj;
            *stop = YES;
        }
    }];
    return config;
}

/// 启动广告
- (void)loadSplashAdWithRootViewController:(UIViewController*)rootViewController{
    
    _splashController = rootViewController;
    
    [[QXAdHandler shareHander] requestAdChannelWithAdType:ADType_Splash block:^(ADChannel channel) {
        switch (channel){
            case ADChannelCSJ:
                [self CSJSplashAd];
                break;
            case ADChannelYLH:
                [self YLHSplashAd];
                break;
            case ADChannelSElF:
                [self SELFSplashAd];
                break;
            default:
                if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdTypeDidLoaded:success:)]){
                    [self.delegate QXAdTypeDidLoaded:ADType_Splash success:NO];
                }
                break;
        }
    }];
}

/// 视频广告
- (void)loadVideoAdWithRootViewController:(UIViewController*)rootViewController{
    
    _videoController = rootViewController;
    [[QXAdHandler shareHander] requestAdChannelWithAdType:ADType_Video block:^(ADChannel channel) {
        switch (channel){
            case ADChannelCSJ:
                [self CSJVideoAd];
                break;
            case ADChannelYLH:
                [self YLHVideoAd];
                break;
            case ADChannelSElF:
                [self SELFVideoAd];
                break;
            default:
                if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdTypeDidLoaded:success:)]){
                    [self.delegate QXAdTypeDidLoaded:ADType_Video success:NO];
                }
                break;
        }
    }];
}

/// 加载Banner广告
- (void)loadBannerAdWithRootViewController:(UIViewController*)rootViewController superView:(UIView*)bannerView{
    
    
    _bannerController = rootViewController;
    _bannerSuper = bannerView;
    
    [[QXAdHandler shareHander] requestAdChannelWithAdType:ADType_Banner block:^(ADChannel channel) {
        switch (channel){
            case ADChannelCSJ:
                [self CSJBannerAd];
                break;
            case ADChannelYLH:
                [self YLHBannerAd];
                break;
            case ADChannelSElF:
                [self SELFBannerAd];
                break;
            default:
                if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdTypeDidLoaded:success:)]){
                    [self.delegate QXAdTypeDidLoaded:ADType_Banner success:NO];
                }
                break;
        }
    }];
}


#pragma mark -- 自有广告
- (void)SELFSplashAd{
    QXSplashAdView* splashAd = [[QXSplashAdView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    splashAd.delegate = self;
    [splashAd loadAdData];
    _splashAd = splashAd;
}

- (void)SELFVideoAd{
    QXFullScreenAdView* videoAd = [[QXFullScreenAdView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    videoAd.delegate = self;
    [videoAd loadAdData];
    _videoAd = videoAd;
}

- (void)SELFBannerAd{
    QXBannerAdView* bannerAd = [[QXBannerAdView alloc] initWithFrame:self.bannerSuper.bounds];
    bannerAd.delegate = self;
    [bannerAd loadAdData];
    _bannerAd = bannerAd;
}

#pragma mark -- QXSplashAdDelegate

- (void)QXSplashAdDidLoaded:(QXSplashAdView *)splashAdView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:ADType_Splash adChannel:ADChannelSElF ad:splashAdView];
    }
    
    if(self.splashController){
        [self.splashController.view addSubview:splashAdView];
    }
}

- (void)QXSplashAdLoadFaild:(QXSplashAdView *)splashAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Splash) adChannel:ADChannelSElF error:NULL];
    }
}

- (void)QXSplashAdDidDisplay:(QXSplashAdView *)splashAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Splash) adChannel:(ADChannelSElF) ad:splashAdView];
    }
}

- (void)QXSplashAdDidClosed:(QXSplashAdView *)splashAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Splash) adChannel:(ADChannelSElF) ad:splashAdView];
    }
}

- (void)QXSplashAdDidClick:(QXSplashAdView *)splashAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Splash) adChannel:(ADChannelSElF) ad:splashAdView];
    }
}


#pragma mark --QXFullScreenAdViewDelegate

- (void)QXFullScreenAdDidLoaded:(QXFullScreenAdView *)fullscreenAd{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:ADType_Video adChannel:ADChannelSElF ad:fullscreenAd];
    }
    
    if(self.videoController){
        [self.videoController.view addSubview:fullscreenAd];
    }
}


- (void)QXFullScreenAdLoadFaild:(QXFullScreenAdView *)fullscreenAd{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Video) adChannel:ADChannelSElF error:NULL];
    }
}


- (void)QXFullScreenAdDidDisplay:(QXFullScreenAdView *)fullscreenAd{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Video) adChannel:(ADChannelSElF) ad:fullscreenAd];
    }
}

- (void)QXFullScreenAdDidClosed:(QXFullScreenAdView *)fullscreenAd{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Video) adChannel:(ADChannelSElF) ad:fullscreenAd];
    }
}

- (void)QXFullScreenAdDidClick:(QXFullScreenAdView *)fullscreenAd{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Video) adChannel:(ADChannelSElF) ad:fullscreenAd];
    }
}

#pragma mark  -- QXBannerViewDelegate

- (void)QXBannerAdDidLoaded:(QXBannerAdView *)bannerAdView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:ADType_Banner adChannel:ADChannelSElF ad:bannerAdView];
    }
    [self.bannerSuper addSubview:bannerAdView];
}


- (void)QXBannerAdLoadFaild:(QXBannerAdView *)bannerAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Banner) adChannel:ADChannelSElF error:NULL];
    }
}


- (void)QXBannerAdDidDisplay:(QXBannerAdView *)bannerAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Banner) adChannel:(ADChannelSElF) ad:bannerAdView];
    }
}

- (void)QXBannerAdDidClosed:(QXBannerAdView *)bannerAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Banner) adChannel:(ADChannelSElF) ad:bannerAdView];
    }
}

- (void)QXBannerAdDidClick:(QXBannerAdView *)bannerAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Banner) adChannel:(ADChannelSElF) ad:bannerAdView];
    }
}

#pragma mark -- 穿山甲广告

-(void)CSJSplashAd{
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    
    BUNativeExpressSplashView *splashAd = [[BUNativeExpressSplashView alloc] initWithSlotID:config.sAdId adSize:[UIScreen mainScreen].bounds.size rootViewController:self.splashController];
    splashAd.tolerateTimeout = 4.0;
    splashAd.delegate = self;
    [splashAd loadAdData];
    _splashAd = splashAd;
    
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:101];
}

-(void)CSJVideoAd{
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    
    BUNativeExpressFullscreenVideoAd *videoAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:config.vAdId];
    videoAd.delegate = self;
    [videoAd loadAdData];
    _videoAd = videoAd;
    
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:101];
}

- (void)CSJBannerAd{

    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    
    BUNativeExpressBannerView* bannerAd = [[BUNativeExpressBannerView alloc] initWithSlotID:config.bAdId rootViewController:self.bannerController  adSize:self.bannerSuper.frame.size interval:30];
    bannerAd.delegate = self;
    [bannerAd loadAdData];
    _bannerAd = bannerAd;
    
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:101];
}



#pragma mark  -- BUNativeExpressSplashViewDelegate

- (void)nativeExpressSplashViewDidLoad:(BUNativeExpressSplashView *)splashAdView{
    
    if(splashAdView.isAdValid){
        if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
            [self.delegate QXAdDidLoaded:(ADType_Splash) adChannel:(ADChannelCSJ) ad:splashAdView];
        }
        if(self.splashController){
            [self.splashController.view addSubview:splashAdView];
        }
        
        QXConfig* config = [self configWithChannel:ADChannelCSJ];
        [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:102];
        
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
            [self.delegate QXAdLoadFaild:(ADType_Splash) adChannel:ADChannelCSJ error:NULL];
        }
    }
}

- (void)nativeExpressSplashView:(BUNativeExpressSplashView *)splashAdView didFailWithError:(NSError * _Nullable)error{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Splash) adChannel:(ADChannelCSJ) error:error];
    }
    
    self.splashAd = NULL;
    self.splashController = NULL;
}

- (void)nativeExpressSplashViewRenderSuccess:(BUNativeExpressSplashView *)splashAdView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Splash) adChannel:ADChannelCSJ ad:splashAdView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:1];
}


- (void)nativeExpressSplashViewRenderFail:(BUNativeExpressSplashView *)splashAdView error:(NSError * __nullable)error{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDisplayFaild:adChannel:error:)]){
        [self.delegate QXAdDisplayFaild:(ADType_Splash) adChannel:ADChannelCSJ error:error];
    }
    
    [splashAdView removeSplashView];
    [splashAdView removeFromSuperview];
    self.splashAd = NULL;
    self.splashController = NULL;
}

- (void)nativeExpressSplashViewDidClick:(BUNativeExpressSplashView *)splashAdView{
   
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Splash) adChannel:(ADChannelCSJ) ad:splashAdView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:2];
}

- (void)nativeExpressSplashViewDidClose:(BUNativeExpressSplashView *)splashAdView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Splash) adChannel:(ADChannelCSJ) ad:splashAdView];
    }
    
    [splashAdView removeSplashView];
    [splashAdView removeFromSuperview];
    self.splashAd = NULL;
    self.splashController = NULL;
}

- (void)nativeExpressSplashViewCountdownToZero:(nonnull BUNativeExpressSplashView *)splashAdView {}


- (void)nativeExpressSplashViewDidClickSkip:(nonnull BUNativeExpressSplashView *)splashAdView {}


- (void)nativeExpressSplashViewDidCloseOtherController:(nonnull BUNativeExpressSplashView *)splashView interactionType:(BUInteractionType)interactionType {}


- (void)nativeExpressSplashViewFinishPlayDidPlayFinish:(nonnull BUNativeExpressSplashView *)splashView didFailWithError:(nonnull NSError *)error {}


- (void)nativeExpressSplashViewWillVisible:(nonnull BUNativeExpressSplashView *)splashAdView {}



#pragma mark - BUNativeExpressFullscreenVideoAdDelegate
//加载成功
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenAd {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:(ADType_Video) adChannel:(ADChannelCSJ) ad:fullscreenAd];
    }
    
    if(self.videoController){
        [fullscreenAd showAdFromRootViewController:self.videoController];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:102];
}

//加载失败
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenAd didFailWithError:(NSError *_Nullable)error {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Video) adChannel:(ADChannelCSJ) error:error];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

//展示成功
- (void)nativeExpressFullscreenVideoAdViewRenderSuccess:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Video) adChannel:(ADChannelCSJ) ad:rewardedVideoAd];
    };
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:1];
}

//展示失败
- (void)nativeExpressFullscreenVideoAdViewRenderFail:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDisplayFaild:adChannel:error:)]){
        [self.delegate QXAdDisplayFaild:(ADType_Video) adChannel:(ADChannelCSJ) error:error];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

//关闭
- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenAd {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Video) adChannel:(ADChannelCSJ) ad:fullscreenAd];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

//被点击
- (void)nativeExpressFullscreenVideoAdDidClick:(BUNativeExpressFullscreenVideoAd *)fullscreenAd {
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Video) adChannel:(ADChannelCSJ) ad:fullscreenAd];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:2];
}

#pragma mark  -- BUNativeExpressBannerViewDelegate

//加载成功
- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:(ADType_Banner) adChannel:(ADChannelCSJ) ad:bannerAdView];
    }
    if(self.bannerSuper){
        bannerAdView.frame = self.bannerSuper.bounds;
        [self.bannerSuper addSubview:bannerAdView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:102];
}

//加载失败
- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *_Nullable)error{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Banner) adChannel:(ADChannelCSJ) error:error];
    }
    self.bannerAd = NULL;
    self.bannerSuper = NULL;
    self.bannerController = NULL;
}

//展示成功
- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Banner) adChannel:ADChannelCSJ ad:bannerAdView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:1];
}

//展示失败
- (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError * __nullable)error{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDisplayFaild:adChannel:error:)]){
        [self.delegate QXAdDisplayFaild:(ADType_Banner) adChannel:(ADChannelCSJ) error:error];
    }
    
    [bannerAdView removeFromSuperview];
    self.bannerAd = NULL;
    self.bannerSuper = NULL;
    self.bannerController = NULL;

}

//移除
- (void)nativeExpressBannerAdViewDidRemoved:(BUNativeExpressBannerView *)bannerAdView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Banner) adChannel:(ADChannelCSJ) ad:bannerAdView];
    }
    
    [bannerAdView removeFromSuperview];
    self.bannerAd = NULL;
    self.bannerSuper = NULL;
    self.bannerController = NULL;
}

//被点击
- (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Banner) adChannel:(ADChannelCSJ) ad:bannerAdView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelCSJ];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:2];
}

#pragma mark --优量汇

-(void)YLHSplashAd{
    
  
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    
    GDTSplashAd* splashAd = [[GDTSplashAd alloc] initWithPlacementId:config.sAdId];
    splashAd.delegate = self;
    splashAd.fetchDelay = 5;
    splashAd.needZoomOut = YES;
    [splashAd loadFullScreenAd];
    _splashAd = splashAd;
    
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:101];
}

-(void)YLHVideoAd{
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
        
    GDTUnifiedInterstitialAd* videoAd = [[GDTUnifiedInterstitialAd alloc] initWithPlacementId:config.vAdId];
    videoAd.delegate = self;
    [videoAd loadFullScreenAd];
    _videoAd = videoAd;
    
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:101];
}

- (void)YLHBannerAd{
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    
    GDTUnifiedBannerView *bannerAd = [[GDTUnifiedBannerView alloc]
                   initWithFrame:self.bannerSuper.bounds
                   placementId:config.bAdId
                   viewController:self.bannerController];
    bannerAd.animated = YES;
    bannerAd.autoSwitchInterval = 30;
    bannerAd.delegate = self;
    [bannerAd loadAdAndShow];
    _bannerAd = bannerAd;
    
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:101];
}
#pragma mark - GDTSplashAdDelegate

//加载成功
- (void)splashAdDidLoad:(GDTSplashAd*)splashAd {

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:(ADType_Splash) adChannel:(ADChannelYLH) ad:splashAd];
    }
    
    if(splashAd.isAdValid){
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [splashAd showFullScreenAdInWindow:keyWindow withLogoImage:NULL skipView:NULL];
    }
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:102];
}

//展示成功
- (void)splashAdSuccessPresentScreen:(GDTSplashAd *)splashAd{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Splash) adChannel:(ADChannelYLH) ad:splashAd];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:1];
}

//展示失败
- (void)splashAdFailToPresent:(GDTSplashAd *)splashAd withError:(NSError *)error{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDisplayFaild:adChannel:error:)]){
        [self.delegate QXAdDisplayFaild:(ADType_Splash) adChannel:(ADChannelYLH) error:error];
    }
    
    self.splashAd = NULL;
    self.splashController = NULL;
}

//关闭
- (void)splashAdClosed:(GDTSplashAd *)splashAd{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Splash) adChannel:(ADChannelYLH) ad:splashAd];
    }
    
    self.splashAd = NULL;
    self.splashController = NULL;
}

//开屏广告点击回调
- (void)splashAdClicked:(GDTSplashAd *)splashAd{

    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Splash) adChannel:(ADChannelYLH) ad:splashAd];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Splash) config:config type:2];
}

#pragma mark  -- GDTUnifiedInterstitialAdDelegate
/**
 *  插屏2.0广告预加载成功回调
 *  当接收服务器返回的广告数据成功且预加载后调用该函数
 */
- (void)unifiedInterstitialSuccessToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:102];
}

/**
 *  插屏2.0广告预加载失败回调
 *  当接收服务器返回的广告数据失败后调用该函数
 */
- (void)unifiedInterstitialFailToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Video) adChannel:(ADChannelCSJ) error:error];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

/**
 *  插屏2.0广告渲染成功
 *  建议在此回调后展示广告
 */
- (void)unifiedInterstitialRenderSuccess:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:(ADType_Video) adChannel:(ADChannelCSJ) ad:unifiedInterstitial];
    }
    if(self.videoController){
        [unifiedInterstitial presentFullScreenAdFromRootViewController:self.videoController];
    }
}

/**
 *  插屏2.0广告渲染失败
 */
- (void)unifiedInterstitialRenderFail:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Video) adChannel:(ADChannelCSJ) error:error];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

/**
 *  插屏2.0广告视图展示成功回调
 *  插屏2.0广告展示成功回调该函数
 */
- (void)unifiedInterstitialDidPresentScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:(ADType_Video) adChannel:(ADChannelYLH) ad:unifiedInterstitial];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:1];
}

/**
 *  插屏2.0广告视图展示失败回调
 *  插屏2.0广告展示失败回调该函数
 */
- (void)unifiedInterstitialFailToPresent:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDisplayFaild:adChannel:error:)]){
        [self.delegate QXAdDisplayFaild:(ADType_Video) adChannel:(ADChannelYLH) error:error];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

/**
 *  插屏2.0广告展示结束回调
 *  插屏2.0广告展示结束回调该函数
 */
- (void)unifiedInterstitialDidDismissScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Video) adChannel:(ADChannelYLH) ad:unifiedInterstitial];
    }
    
    self.videoAd = NULL;
    self.videoController = NULL;
}

/**
 *  插屏2.0广告点击回调
 */
- (void)unifiedInterstitialClicked:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Video) adChannel:(ADChannelYLH) ad:unifiedInterstitial];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Video) config:config type:2];
}


#pragma mark -- GDTUnifiedBannerViewDelegate

- (void)unifiedBannerViewDidLoad:(GDTUnifiedBannerView *)unifiedBannerView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidLoaded:adChannel:ad:)]){
        [self.delegate QXAdDidLoaded:(ADType_Banner) adChannel:(ADChannelYLH) ad:unifiedBannerView];
    }
    
    [self.bannerSuper addSubview:unifiedBannerView];
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:102];
}

- (void)unifiedBannerViewFailedToLoad:(GDTUnifiedBannerView *)unifiedBannerView error:(NSError *)error{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdLoadFaild:adChannel:error:)]){
        [self.delegate QXAdLoadFaild:(ADType_Banner) adChannel:(ADChannelYLH) error:error];
    }
    
    self.bannerAd = NULL;
    self.bannerSuper = NULL;
    self.bannerController = NULL;
}

/**
 *  banner2.0曝光回调
 */
- (void)unifiedBannerViewWillExpose:(GDTUnifiedBannerView *)unifiedBannerView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidDisplayed:adChannel:ad:)]){
        [self.delegate QXAdDidDisplayed:ADType_Banner adChannel:ADChannelYLH ad:unifiedBannerView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:1];
}

- (void)unifiedBannerViewClicked:(GDTUnifiedBannerView *)unifiedBannerView{
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClicked:adChannel:ad:)]){
        [self.delegate QXAdDidClicked:(ADType_Banner) adChannel:(ADChannelYLH) ad:unifiedBannerView];
    }
    
    QXConfig* config = [self configWithChannel:ADChannelYLH];
    [[QXAdHandler shareHander] adCallBackAdType:(ADType_Banner) config:config type:2];
}

- (void)unifiedBannerViewWillClose:(nonnull GDTUnifiedBannerView *)unifiedBannerView {
 
    if(self.delegate && [self.delegate respondsToSelector:@selector(QXAdDidClosed:adChannel:ad:)]){
        [self.delegate QXAdDidClosed:(ADType_Banner) adChannel:(ADChannelYLH) ad:unifiedBannerView];
    }
    
    [unifiedBannerView removeFromSuperview];
    self.bannerAd = NULL;
    self.bannerSuper = NULL;
    self.bannerController = NULL;
}
@end
